package handlers

import (
	"gin-blog/models"
	"gin-blog/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func UserLogin(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	//对于用户名和密码的基础校验
	if username == "" || password == "" {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "用户名或者密码为空",
		})
		return
	}

	//判断用户账户是否存在
	var user models.User
	err := models.DB.Where("username = ?", username).First(&user).Debug().Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}

	//判断密码是否正确
	if user.Password != pkg.Md5Str(password) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}

	token, _ := pkg.TokenHandler(strconv.Itoa(int(user.ID)))
	c.JSON(http.StatusOK, gin.H{
		"code":  200,
		"msg":   "登录成功",
		"token": token,
	})
	return

}

// UserGetToken 解密token
func UserGetToken(c *gin.Context) {

	uid, _ := c.Get("uid") //获取uid参数

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"uid":  uid,
	})
	return
}

//func UserLogin(c *gin.Context) {
//	username := c.PostForm("username")
//	password := c.PostForm("password")
//	user := models.User{
//		Username: username,
//		Password: password,
//	}
//	models.DB.Where("username = ?", user.Username).First(&user)
//	if user.ID == 0 {
//		c.JSON(http.StatusOK, gin.H{
//			"code": 500,
//			"msg":  "用户不存在",
//		})
//		return
//	}
//	if user.Password != pkg.Md5Str(password) {
//		c.JSON(http.StatusOK, gin.H{
//			"code": 500,
//			"msg":  "密码错误",
//		})
//		return
//	}
//
//	token, _ := pkg.TokenHandler(strconv.Itoa(int(user.ID)))
//	c.JSON(http.StatusOK, gin.H{
//		"code":  200,
//		"msg":   "success",
//		"user":  user,
//		"token": token,
//	})
//	return
//}

func UserLoginGet(c *gin.Context) {
	uid, _ := c.Get("uid")
	var user []models.User
	models.DB.Find(&user)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"uid":  uid,
		"data": user,
	})
	return
}

func UserLoginPost(c *gin.Context) {
	uid, _ := c.Get("uid")
	var user []models.User
	models.DB.Find(&user)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"uid":  uid,
		"data": user,
	})
	return
}

func UserDel(c *gin.Context) {
	id := c.Query("id")
	err := models.DB.Delete(&models.User{}, id).Debug().Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "删除失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
	})
	return
}

func UserDetail(c *gin.Context) {
	id := c.Query("id")
	var user models.User
	err := models.DB.Where("id=?", id).First(&user).Debug().Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": user,
	})
	return
}

func UserList(c *gin.Context) {
	//page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) //设置默认值
	//size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var count int64
	models.DB.Model(models.User{}).Count(&count)

	var user []models.User
	models.DB.Offset(offset).Limit(size).Find(&user)

	c.JSON(http.StatusOK, gin.H{
		"code":  200,
		"data":  user,
		"count": count,
	})
	return
}

func UserAdd(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	user := models.User{
		Username: username,
		Password: password,
	}
	err := models.DB.Create(&user).Debug().Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加ok",
		"data": user,
	})
	return
}
